package ezCall;

import javax.microedition.pim.Contact;

import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;



public class List_PopupScreen extends PopupScreen implements ListFieldCallback 
{

	private ListField  _Menu_ListField;
	private LabelField _MenuLabel_Field;
	
	
	private String[] _MenuString;
	private String[] _MenuHeaderString;
	
	private int _List_Mode;
	public final int LIST_WITHOUT_HEADER = 0;
	public final int LIST_WITH_HEADER    = 1;
	
	private int _SelectedIndex;
	protected boolean _OK;
	
	protected int _HeaderWidth;
	
	public List_PopupScreen( String[] StrArray )
    {        	
        super( new VerticalFieldManager(), DEFAULT_CLOSE );    
        
        _Menu_ListField = new ListField();
        _Menu_ListField.setCallback(this);  
        
        _MenuString = StrArray;
        
        _Menu_ListField.setSize( _MenuString.length );
        
        add(_Menu_ListField);
        
        _List_Mode = LIST_WITHOUT_HEADER;
        
        _OK = false;
    }

	public List_PopupScreen( String[] StrHeaderArray, String[] StrArray )
    {        	
        super( new VerticalFieldManager(), DEFAULT_CLOSE );    
        
        _Menu_ListField = new ListField();
        _Menu_ListField.setCallback(this);  
        
        _MenuHeaderString = StrHeaderArray;
        _MenuString = StrArray;
        
        _Menu_ListField.setSize( _MenuString.length );
        
        add(_Menu_ListField);
        
        _List_Mode = LIST_WITH_HEADER;
        _HeaderWidth = 40;
        
        _OK = false;
    }	

	public void Set_ListSelectedIndex( int Index )
	{
		_Menu_ListField.setSelectedIndex(Index);
	}
	
	
    protected boolean navigationMovement(int dx, int dy, int status, int time )
    {
    	return super.navigationMovement(dx, dy, status, time);
    }
    
    
    
    protected  void sublayout(int width, int height)
    {
    	
    	super.sublayout(width, height);    	
    	//setPosition(0, 0);
    }
    
    public String Get_SelectedListString()
    {
    	String ListStr = new String();
    	
    	if( _Menu_ListField.getSelectedIndex() >= 0 )
    	{
    		ListStr = _MenuString[_Menu_ListField.getSelectedIndex()];
    	}
    	
    	return ListStr;
    }
	
    
    public int Get_SelectedListIndex()
    {
    	return _Menu_ListField.getSelectedIndex();
    }
    
    
    
    public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) 
    {

        if(listField == _Menu_ListField && index < _MenuString.length )
        {
        	if( _List_Mode == LIST_WITHOUT_HEADER )
        	{
        		graphics.drawText( _MenuString[index], 0, y );
        	}
        	else
        	if( _List_Mode == LIST_WITH_HEADER )
        	{
        		int HeaderWith = width * _HeaderWidth/100;
        		int StrWidth   = width * (100-_HeaderWidth)/100;
        		int StrPos    = HeaderWith;
        		graphics.drawText( _MenuHeaderString[index], 0, y, Graphics.ELLIPSIS, StrWidth );
        		graphics.drawText( _MenuString[index], StrPos, y );
        		
        	}

        
        }
        
    }
    
    public void close()
    {
    	_SelectedIndex = Get_SelectedListIndex();
    	
    	super.close();
    }

    public int Get_SavedSelectedIndex()
    {
    	return _SelectedIndex;
    }
    
    public boolean Get_SavedOK()
    {
    	return _OK;
    }
    
    public Object get(ListField listField, int index)
    {
    	return _MenuString[index];
    }
    

    public int getPreferredWidth(ListField listField) 
    {
    	return Graphics.getScreenWidth();
    }
 
   
    public int indexOfList(ListField listField, String prefix, int start) 
    {
        return -1; // Not implemented.
    }    	
}